//
//  UIColor+Hex.m
//  GFrameWork
//
//  Created by gehaipeng on 14-8-13.
//  Copyright (c) 2014年 Gable. All rights reserved.
//

#import "UIColor+Hex.h"

@implementation UIColor (Hex)



+ (UIColor *)colorWithRGBHex:(UInt32)hex alpha:(float)opacity{
    
	int r = (hex >> 16) & 0xFF;
	int g = (hex >> 8) & 0xFF;
	int b = (hex) & 0xFF;
    return [UIColor colorWithRed:r / 255.0f
						   green:g / 255.0f
							blue:b / 255.0f
						   alpha:opacity];
}

+(UIColor *)colorWithHexString:(NSString *)stringToConvert  {
    
	NSScanner *scanner = [NSScanner scannerWithString:stringToConvert];
	unsigned hexNum;
    
	if (![scanner scanHexInt:&hexNum]) return nil;
    
	return [UIColor colorWithRGBHex:hexNum alpha:1.0];
    
}

+(UIColor *)colorWithHex:(NSString *)hexStr alpha:(float)opacity
{
	NSScanner *scanner = [NSScanner scannerWithString:hexStr];
	unsigned hexNum;
	if (![scanner scanHexInt:&hexNum]) return nil;
	return [UIColor colorWithRGBHex:hexNum alpha:opacity];

}

+(UIImage *)imageFromColor:(CGRect )frame andColor:(UIColor *)color
{
    UIGraphicsBeginImageContext(frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context,
                                   [color CGColor]);
    CGContextFillRect(context, frame);
    UIImage * imge = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return imge;
}

@end
